package com.example.ttkl.fragment.XYXActivity.PT;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import com.example.ttkl.Constants;
import com.example.ttkl.MyApplication;
import com.example.ttkl.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;

public class GameStartActivity extends AppCompatActivity {

    Button mAGS;
    LinearLayout mChooseFromAlbum,mLLGameStartChooseDiff,mSelected = null;
    ImageView mPicture1,mPicture2,mPicture3,mPicture4,mPicture5,mback;
    TextView mLevel;
    Intent mIntent;
    int pictureIndex;
    ArrayList<String> mPicturePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_start);
        mChooseFromAlbum = findViewById(R.id.activity_game_start_choose_from_album);
        mPicture1 = findViewById(R.id.activity_game_start_picture1);
        mPicture2 = findViewById(R.id.activity_game_start_picture2);
        mPicture3 = findViewById(R.id.activity_game_start_picture3);
        mPicture4 = findViewById(R.id.activity_game_start_picture4);
        mPicture5 = findViewById(R.id.activity_game_start_picture5);
        mLLGameStartChooseDiff = findViewById(R.id.activity_game_start_choose_difficulty);
        mback = findViewById(R.id.activity_game_start_back);
        mLevel = findViewById(R.id.activity_game_start_level);
        mAGS = findViewById(R.id.activity_game_start_button);
        mPicture1.setOnClickListener(this::onClickPicture);
        mPicture2.setOnClickListener(this::onClickPicture);
        mPicture3.setOnClickListener(this::onClickPicture);
        mPicture4.setOnClickListener(this::onClickPicture);
        mPicture5.setOnClickListener(this::onClickPicture);
        mLLGameStartChooseDiff.setOnClickListener(this::onClickPicture);
        mback.setOnClickListener(this::onClickPicture);
        mAGS.setOnClickListener(this::onClickPicture);
        mChooseFromAlbum.setOnClickListener(this::onClickPicture);
        EventBus.getDefault().register(this);
        setLevel();
        mPicturePath = new ArrayList<>();
        mPicturePath.add(MyApplication.getPinTu()+"/"+ Constants.tupian+"/pt1.png");
        mPicturePath.add(MyApplication.getPinTu()+"/"+ Constants.tupian+"/pt2.png");
        mPicturePath.add(MyApplication.getPinTu()+"/"+ Constants.tupian+"/pt3.png");
        mPicturePath.add(MyApplication.getPinTu()+"/"+ Constants.tupian+"/pt4.png");
        mPicturePath.add(MyApplication.getPinTu()+"/"+ Constants.tupian+"/pt5.png");
        pictureIndex = 0;
        setPicture();
    }

    void setLevel() {
        String[] levels = getResources().getStringArray(R.array.difficulty);
        mLevel.setText(levels[MyApplication.getLevel() - 3]);
    }

    void setPicture() {
        File f1 = new File(mPicturePath.get(0));
        mPicture1.setImageURI(Uri.fromFile(f1));
        File f2 = new File(mPicturePath.get(1));
        mPicture2.setImageURI(Uri.fromFile(f2));
        File f3 = new File(mPicturePath.get(2));
        mPicture3.setImageURI(Uri.fromFile(f3));
        File f4 = new File(mPicturePath.get(3));
        mPicture4.setImageURI(Uri.fromFile(f4));
        File f5 = new File(mPicturePath.get(4));
        mPicture5.setImageURI(Uri.fromFile(f5));
    }

    void onClickPicture(View view) {
        mIntent = GameActivity.getIntent(GameStartActivity.this);
        switch (view.getId()) {
            case R.id.activity_game_start_picture1:
                resetSelected();
                setSelected((LinearLayout) mPicture1.getParent());
                pictureIndex = 0;
                break;
            case R.id.activity_game_start_picture2:
                resetSelected();
                setSelected((LinearLayout) mPicture2.getParent());
                pictureIndex = 1;
                break;
            case R.id.activity_game_start_picture3:
                resetSelected();
                setSelected((LinearLayout) mPicture3.getParent());
                pictureIndex = 2;
                break;
            case R.id.activity_game_start_picture4:
                resetSelected();
                setSelected((LinearLayout) mPicture4.getParent());
                pictureIndex = 3;
                break;
            case R.id.activity_game_start_picture5:
                resetSelected();
                setSelected((LinearLayout) mPicture5.getParent());
                pictureIndex = 4;
                break;
            case R.id.activity_game_start_choose_from_album:
                choosePicture();
                pictureIndex = 5;
                break;
            case R.id.activity_game_start_choose_difficulty:
                FragmentManager manager = getSupportFragmentManager();
                ChooseDifficultyFragment dialog = new ChooseDifficultyFragment();
                dialog.setChooseDifficultyFragmentListener(new ChooseDifficultyFragment.ChooseDifficultyFragmentListener() {
                    @Override
                    public void onChooseDifficulty(int index) {
                        String[] levels = getResources().getStringArray(R.array.difficulty);
                        mLevel.setText(levels[index]);
                        MyApplication.setLevel(index + 3);
                    }
                });
                dialog.show(manager, "singleDialog");
                break;
            case R.id.activity_game_start_button:
                mIntent.putExtra("picturePath", mPicturePath.get(pictureIndex));
                startActivity(mIntent);
                break;
            case R.id.activity_game_start_back:
                finish();
                break;
        }
    }

    private void choosePicture() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, Utility.REQUEST_CHOOSE_PICTURE_CODE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Utility.REQUEST_CHOOSE_PICTURE_CODE) {
            if (resultCode == RESULT_OK) {
                Intent intent = ClipPictureActivity.getIntent(GameStartActivity.this);
                intent.setData(data.getData());
                startActivity(intent);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    public void onPictureClipFinish(PictureClipFinishEvent event) {
        mPicturePath.add(event.getPicturePath());
        mChooseFromAlbum.removeAllViews();
        ImageView clipPicture = new ImageView(GameStartActivity.this);
        clipPicture.setImageBitmap(event.getBitmap());
        mChooseFromAlbum.addView(clipPicture);
        resetSelected();
        setSelected((LinearLayout) mChooseFromAlbum.getParent());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    void resetSelected() {
        if (mSelected != null) {
            mSelected.setBackgroundResource(0);
        }
    }

    void setSelected(LinearLayout selected) {
        mSelected = selected;
        mSelected.setBackgroundResource(R.drawable.blue_shadow);
    }
}
